HW 03

Author

Cedric Destin

options(warn=-1)
if (!require("pacman")) 
  install.packages("pacman")

# use this line for installing/loading
pacman::p_load(tidyverse,
               glue,
               scales,
               openintro,
               gridExtra,
               ggrepel,
               ggmap,
               ggridges,
               dsbox,
               devtools,
               fs,
               janitor,
               here,
               dplyr,
               palmerpenguins,
               stringr,
               ggplot2,
               plotly) 

1 - Du Bois challenge.

income <- read_csv(here("data", "income.csv"), 
                    show_col_types = FALSE)
income <- income |> 
  mutate(
      Rent_pct = Rent / 100,
      Food_pct = Food / 100,
      Clothes_pct = Clothes / 100,
      Tax_pct = Tax / 100,
      Other_pct = Other / 100
    )
income <- income %>%
  mutate(Class = paste0(as.character(Class), "   $", as.character(Average_Income)))
income$Class <- factor(income$Class, levels = c("$1000 AND OVER   $1125", "$750-1000   $880", "$500-750   $547", "$400-500   $433.82", "$300-400   $335.66", "$200-300   $249.45", "$100-200   $139.1"))
glimpse(income)
Rows: 7
Columns: 12
$ Class          <fct> $100-200   $139.1, $200-300   $249.45, $300-400   $335.…
$ Average_Income <dbl> 139.10, 249.45, 335.66, 433.82, 547.00, 880.00, 1125.00
$ Rent           <dbl> 19, 22, 23, 18, 13, 0, 0
$ Food           <dbl> 43, 47, 43, 37, 31, 37, 29
$ Clothes        <dbl> 28, 23, 18, 15, 17, 19, 16
$ Tax            <dbl> 9.9, 4.0, 4.5, 5.5, 5.0, 8.0, 4.5
$ Other          <dbl> 0.1, 4.0, 11.5, 24.5, 34.0, 36.0, 50.5
$ Rent_pct       <dbl> 0.19, 0.22, 0.23, 0.18, 0.13, 0.00, 0.00
$ Food_pct       <dbl> 0.43, 0.47, 0.43, 0.37, 0.31, 0.37, 0.29
$ Clothes_pct    <dbl> 0.28, 0.23, 0.18, 0.15, 0.17, 0.19, 0.16
$ Tax_pct        <dbl> 0.099, 0.040, 0.045, 0.055, 0.050, 0.080, 0.045
$ Other_pct      <dbl> 0.001, 0.040, 0.115, 0.245, 0.340, 0.360, 0.505
# Your original code, modified to include text labels
fig <- plot_ly(income, x = ~Rent, y = ~Class, 
               type = 'bar', 
               orientation = 'h',
               name = 'Rent',
               # --- Add these lines ---
               text = ~Rent_pct,                 # Use the percentage column for the text data
               textposition = 'inside',
               texttemplate = '%{text:.0%}',      # Format the text as a percentage with 0 decimal places
               # ----------------------
               marker = list(color = '#121210'))

fig <- fig %>% add_trace(x = ~Food, name = 'Food',
                         # --- Add these lines ---
                         text = ~Food_pct,
                         textposition = 'inside',
                         texttemplate = '%{text:.0%}',
                         # ----------------------
                         marker = list(color = '#7D5A7F'))

fig <- fig %>% add_trace(x = ~Clothes, name = 'Clothes',
                         # --- Add these lines ---
                         text = ~Clothes_pct,
                         textposition = 'inside',
                         texttemplate = '%{text:.0%}',
                         # ----------------------
                         marker = list(color = '#D79684'))

fig <- fig %>% add_trace(x = ~Tax, name = 'Tax',
                         # --- Add these lines ---
                         text = ~Tax_pct,
                         textposition = 'inside',
                         texttemplate = '%{text:.1%}',
                         # ----------------------
                         marker = list(color = '#003e80'))

fig <- fig %>% add_trace(x = ~Other, name = 'Other',
                         # --- Add these lines ---
                         text = ~Other_pct,
                         textposition = 'inside',
                         texttemplate = '%{text:.1%}',
                         # ----------------------
                         marker = list(color = '#e6f2ff'))

# Apply the layout (no changes needed here)
fig <- fig %>% layout(
  barmode = 'stack',
  title = "INCOME AND EXPENITURE OF 150 NEGRO FAMILIES IN ATLANTA, GA. U.S.A.",
  titlefont = list(size = 15, color = "#000000"),
  xaxis = list(
    title = "FOR FUTHER STATISTICS RAISE THIS FRAME",
    showticklabels = FALSE
  ),
    annotations = list(
      list(
        x = -0.28,
        y = 1.025,
        text = "Class     Actual Average",
        showarrow = FALSE,
        xref = "paper",
        yref = "paper"
      )
    ),
  yaxis = list(title = ""),
  showlegend = FALSE,
  plot_bgcolor = "#CAB2A0",   # inside plot area
  paper_bgcolor = "#CAB2A0"   # outside plot area
)

# Display the figure
fig
library(plotly)

# Sample data
data <- data.frame(
  x = 1:5,
  y = c(10, 15, 13, 17, 20)
)

# Create the plot
fig <- plot_ly(data, x = ~x, y = ~y, type = 'scatter', mode = 'lines')

# Update layout with increased margins
fig <- fig %>%
  layout(
    margin = list(l = 50, r = 50, b = 50, t = 100, pad = 4)
  )

# Add text annotation outside the plot
fig <- fig %>%
  layout(
    annotations = list(
      list(
        x = 0.5,
        y = 1.1,
        text = "This text is outside the plot area",
        showarrow = FALSE,
        xref = "paper",
        yref = "paper"
      )
    )
  )

# Print the plot
fig

2 - COVID survey - interpret

3 - COVID survey - reconstruct

4 - COVID survey - re-reconstruct

5 - COVID survey - another view